

package com.io.controller;

import com.io.constant.Constant;
import com.io.dto.ApiResult;
import com.io.entity.SysDeptEntity;
import com.io.service.SysDeptService;
import com.io.utils.annotation.DataFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


/**
 * 部门管理
 *
 * @author guodd
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends BaseController {
    @Autowired
    private SysDeptService sysDeptService;

    @ApiOperation("部门列表")
    @GetMapping("/list")
    @RequiresPermissions("sys:dept:list")
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public ApiResult list() {
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());
        return ApiResult.ok(deptList);
    }

    @ApiOperation("选择部门(添加、修改菜单)")
    @GetMapping("/select")
    @RequiresPermissions("sys:dept:select")
    public ApiResult select() {
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());
        // 添加一级部门
        SysDeptEntity root = new SysDeptEntity();
        if (getUserId() == Constant.SUPER_ADMIN) {
            root.setDeptId(0L);
            root.setName("一级部门");
            root.setParentId(0L);
            root.setOpen(true);
            root.setChildren(deptList);
        }
        return ApiResult.ok(root);
    }

    @ApiOperation("上级部门Id(管理员则为0)")
    @GetMapping("/info")
    @RequiresPermissions("sys:dept:list")
    public ApiResult info() {
        long deptId = 0;
        if (getUserId() != Constant.SUPER_ADMIN) {
            List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());
            Long parentId = null;
            for (SysDeptEntity sysDeptEntity : deptList) {
                if (parentId == null) {
                    parentId = sysDeptEntity.getParentId();
                    continue;
                }

                if (parentId > sysDeptEntity.getParentId()) {
                    parentId = sysDeptEntity.getParentId();
                }
            }
        }
        return ApiResult.ok(deptId);
    }

    @ApiOperation("部门信息")
    @PostMapping("/info/{deptId}")
    @RequiresPermissions("sys:dept:info")
    public ApiResult info(@PathVariable("deptId") Long deptId) {
        SysDeptEntity dept = sysDeptService.getById(deptId);
        return ApiResult.ok().put("dept", dept);
    }

    @ApiOperation("保存部门信息")
    @PostMapping("/save")
    @RequiresPermissions("sys:dept:save")
    public ApiResult save(@RequestBody SysDeptEntity dept) {
        sysDeptService.save(dept);
        return ApiResult.ok();
    }

    @ApiOperation("修改部门信息")
    @PostMapping("/update")
    @RequiresPermissions("sys:dept:update")
    public ApiResult update(@RequestBody SysDeptEntity dept) {
        sysDeptService.updateById(dept);
        return ApiResult.ok();
    }

    @ApiOperation("删除部门信息")
    @PostMapping("/delete")
    @RequiresPermissions("sys:dept:delete")
    public ApiResult delete(long deptId) {
        //判断是否有子部门
        List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
        if (deptList.size() > 0) {
            return ApiResult.error("请先删除子部门");
        }
        sysDeptService.removeById(deptId);
        return ApiResult.ok();
    }

}
